package Model;

import Utils.TimeFormat;
import java.util.HashMap;
import java.util.GregorianCalendar;

/**
 * Class which represents the sale orders
 */
public class SaleOrder {
	private int id;
	private GregorianCalendar date;
	private HashMap<Product, Integer> products;
	private String deliveryStatus;
	private GregorianCalendar deliveryDate;
	private Customer customer;
	private Employee employee;
	private Invoice invoice;
	private double discount;

	public SaleOrder() { date = new GregorianCalendar(); products = new HashMap<Product, Integer>(); }
	public SaleOrder(int id) { date = new GregorianCalendar(); this.id = id; products = new HashMap<Product, Integer>(); }
	public SaleOrder(String delivery_date, String delivery_status, Customer customer, Employee employee, Invoice invoice) {
		date = new GregorianCalendar();
		this.deliveryDate = TimeFormat.string_to_gregorian(delivery_date);
		this.deliveryStatus = delivery_status;
		this.customer = customer;
		this.employee = employee;
		this.invoice = invoice;
		products = new HashMap<Product, Integer>();
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the date
	 */ public String getDate() {
		return TimeFormat.gregorian_to_string(date);
	}

	/**
	 * @param date the date to set
	 */ public void setDate(String date) {
		this.date = TimeFormat.string_to_gregorian(date);
	}

	/**
	 * @return the products
	 */ public HashMap<Product, Integer> getProducts() {
		return products;
	}

	/**
	 * @param products the products to set
	 */ public void setProducts(HashMap<Product, Integer> products) {
		this.products = products;
	}
	 public void addProduct(Product product, int amount) {
		 products.put(product, amount);
	 }
	 public void deleteProduct(Product product, int amount) throws Exception {
		 int total_amount = products.get(product);
		 if (amount < 0)
			 throw new Exception();
		 else if (total_amount < amount)
			 products.remove(product);
		 else {
			 int new_amount = total_amount - amount;
			 products.put(product, new_amount);
		 }
	 }

	/**
	 * @return the deliveryStatus
	 */ public String getDeliveryStatus() {
		return deliveryStatus;
	}

	/**
	 * @param deliveryStatus the deliveryStatus to set
	 */ public void setDeliveryStatus(String deliveryStatus) {
		this.deliveryStatus = deliveryStatus;
	}

	/**
	 * @return the deliveryDate
	 */ public String getDeliveryDate() {
		return (deliveryDate == null ? null : TimeFormat.gregorian_to_string(deliveryDate));
	}

	/**
	 * @param deliveryDate the deliveryDate to set
	 */ public void setDeliveryDate(GregorianCalendar deliveryDate) {
		this.deliveryDate = deliveryDate;
	}

	/**
	 * @return the customer
	 */ public Customer getCustomer() {
		return customer;
	}

	/**
	 * @param customer the customer to set
	 */ public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	/**
	 * @return the employee
	 */ public Employee getEmployee() {
		return employee;
	}

	/**
	 * @param employee the employee to set
	 */ public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	/**
	 * @return the discount
	 */ public double getDiscount() {
		return discount;
	}

	/**
	 * @param discount the discount to set
	 */ public void setDiscount(double discount) {
		this.discount = discount;
	}

	 public void setInvoice(Invoice invoice) {
		 this.invoice = invoice;
	 }
	 public Invoice getInvoice() {
		 return invoice;
	 }
}
